package com.whut.transportation.overweight;


import java.util.HashMap;


import com.whut.transportation.R;
import com.whut.transportation.SearchTrainActivity;
import com.whut.transportation.SearchTransportationActivity;
import com.whut.transportation.R.layout;
import com.whut.transportation.entity.AllTrainInfo;
import com.whut.transportation.entity.TrainInfo;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.ui.BaseActivity.DataCallback;
import com.whut.transportation.utils.Constant;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;



public class OWSearchActivity extends BaseActivity {

	private TextView spinnerText1;
	private TextView spinnerText3;
	private Spinner spinner01;//搜索信息下拉条
	private Spinner spinner03;//列车车号下拉条
	private Spinner spinnerTime;//时间段选择下拉条
	private Button searchSubmit;
	private EditText search_con;
	
	private ArrayAdapter arrayAdapter1;//第一个下拉条的适配器
	private ArrayAdapter arrayAdapterTime;//第二个下拉条的适配器，时间
	private ArrayAdapter<String> arrayAdapter3;//第三个下拉条的适配器
	
	private String searchInfomation;//第一个下拉条选中的信息
	private String searchTime;//第二个下拉条选中的信息
	private String trainId;//第三个下拉条选中的信息
	private TrainInfo train;//存车辆信息
	
	private SharedPreferences preferences;//取出偏好信息
	
	
	@Override
	protected void loadViewLayout() {

		setContentView(R.layout.activity_owsearch);
		setTitleBarView(false, "搜索", -1, false);

		preferences=getSharedPreferences("userId", Context.MODE_PRIVATE);
		searchInfomation = getResources().getStringArray(R.array.owinfomation)[0];
	}
	
	@Override
	protected void findViewById() {
		spinnerText3 = (TextView) findViewById(R.id.spinnerText3);
		spinner01 = (Spinner) findViewById(R.id.Spinner01);
		spinner03 = (Spinner) findViewById(R.id.Spinner03);
		spinnerTime = (Spinner) findViewById(R.id.spinnerTime);
		searchSubmit = (Button) findViewById(R.id.search_submit);
		search_con = (EditText) findViewById(R.id.search_con);
	}
	
	@Override
	protected void afterFindViewId() {

		//从arrays.xml中取出information放入第一个下拉条中
		arrayAdapter1 = ArrayAdapter.createFromResource(mContext, R.array.owinfomation, android.R.layout.simple_spinner_item);
		arrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner01.setAdapter(arrayAdapter1);
		spinner01.setVisibility(View.VISIBLE);
		
		arrayAdapterTime = ArrayAdapter.createFromResource(mContext, R.array.time, android.R.layout.simple_spinner_item);
		arrayAdapterTime.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerTime.setAdapter(arrayAdapterTime);
		spinnerTime.setVisibility(View.VISIBLE);
	}

	@Override
	protected void processLogic() {

		//查询所有车辆，将车号放入第二个下拉条中
		HttpRequestVo vo = new HttpRequestVo();
		vo.requestUrl = Constant.FIND_ALL_TRAIN_INFO;
		vo.context = mContext;
		vo.parser = new JSONParser(AllTrainInfo.class);
		vo.requestDataMap = null;
		getDataFromServer(HttpManager.GET_MOTHOD, vo, 
				new DataCallback<HashMap<String, Object>>() {
					@Override
					public void onStart() {
					}
					@Override
					public void onFailed() {
						showNetErrorDialog();
					}
					@Override
					public void processData(
							HashMap<String, Object> paramObject,
							boolean paramBoolean) {
						int code = (Integer) paramObject.get("code");
						if(code == 1){
							AllTrainInfo allTranInfos = (AllTrainInfo) paramObject.get("result");
							String[] trainIds = new String[allTranInfos.ListTrainInfo.size()+1];
							//第二个下拉条的第一条信息为"不限"
							trainIds[0] = "不限";
							//将数据存入数组中
							int i = 1;
							for(TrainInfo t : allTranInfos.ListTrainInfo){
								trainIds[i] = t.getTrainId().trim();
								i++;
							}
							trainId = trainIds[0];
							arrayAdapter3 = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item,trainIds);
							arrayAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
							spinner03.setAdapter(arrayAdapter3);
							spinner03.setVisibility(View.VISIBLE);
						}
					}
					@Override
					public void onFinish() {
						closeProgressDialog();
					}
				});
	}


	@Override
	protected void setListener() {
		searchSubmit.setOnClickListener(this);
		spinner01.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				//第一个下拉条改变时，改变searchInfomation的值
				searchInfomation = arg0.getItemAtPosition(arg2).toString();
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		spinnerTime.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				//第二个下拉条改变时，改变searchTime的值
				searchTime = arg0.getItemAtPosition(arg2).toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
		spinner03.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				//第三个下拉条的值改变时，改变trainId的值
				trainId = arg0.getItemAtPosition(arg2).toString();
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}

	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.search_submit:
			searchSubmit();
			break;
		default:
			break;
		}
		super.onClick(v);
	}
	
	private void searchSubmit() {
		//取出输入框输入的信息
		String searchContext = search_con.getText().toString();
		//查询输入信息和列车信息是由searchInfomation决定的
		if("超重处理信息".equals(searchInfomation)){
			Intent intent = new Intent(this,OWSearchOverWeighActivity.class);
			//当第三个下拉条选择的是“不限”时，查找的是所有信息
			if("不限".equals(trainId))
				trainId = "";
			intent.putExtra("trainId", trainId);
			//时间条件，选中不同的条目用不同的数字代替
			if("不限".equals(searchTime))
				searchTime="0";
			else if("1天内".equals(searchTime))
				searchTime="1";
			else if("1周内".equals(searchTime))
				searchTime="7";
			else if("1月内".equals(searchTime))
				searchTime="30";
			else if("3月内".equals(searchTime))
				searchTime="90";
			else if("6月内".equals(searchTime))
				searchTime="180";
			else if("1年内".equals(searchTime))
				searchTime="360";
			//传值
			intent.putExtra("searchTime", searchTime);
			if(searchContext.length()<=0)
			searchContext = "";
			intent.putExtra("searchContext", searchContext);
			startActivity(intent);
		} else if("列车信息".equals(searchInfomation)){
			Intent intent = new Intent(this,SearchTrainActivity.class);
			intent.putExtra("trainId", trainId);
			startActivity(intent);
		}
	}


	@Override
	protected void getDataAgain() {
		
	}

}
